package com.shopManager.contoller.shop;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.bean.Shop;
import com.shop.feign.ShopFeign;

import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;


@Controller
@RequestMapping("/shopManager/")
public class ShopManagerContoller {

	
	@Autowired
	ShopFeign  shopFeign;
	
	@ResponseBody
	@RequestMapping("/insert.shtml")
	public RestResult insert( Shop shop) {
		if(StringUtils.isBlank(shop.getContent())) {
			return RestResultGenerator.genErrorResult("商品内容不能为空", null, Status.shop_content_is_empty);
		}
		if(shop.getPrice() == null ||shop.getPrice().doubleValue()<=0) {
			return RestResultGenerator.genErrorResult("商品金额不正确", null, Status.shop_price_is_error);
		}
		if(shop.getScore() ==null ||shop.getScore().doubleValue() <=0) {
			return RestResultGenerator.genErrorResult("商品积分不正确", null, Status.shop_score_is_error);
		}
		if(StringUtils.isBlank(shop.getSimpleDesc())) {
			return RestResultGenerator.genErrorResult("商品简介不能为空", null, Status.shop_simple_content_is_empty);
		}
		if(StringUtils.isBlank(shop.getTitle())) {
			return RestResultGenerator.genErrorResult("商品标题不能为空", null, Status.shop_title_is_empty);
		}
		if(shop.getType() == null) {
			return RestResultGenerator.genErrorResult("商品类型不能为空", null, Status.shop_type_is_empty); 
		}
		RestResult result = this.shopFeign.insert(shop);;
		if(result == null) {
			return RestResultGenerator.genError("商品添加失败,服务不可用");
		}
		return result;
	}
}
